Skip to main content

Common NumPy Methods

🔨 Array Creation

  • np.array() → create array from list/tuple
np.array([1, 2, 3])
  • np.zeros() / np.ones() → arrays filled with 0 or 1
np.zeros((3, 4))
np.ones((2, 3))
  • np.eye() → identity matrix
np.eye(4)
  • np.arange() → range of values
np.arange(0, 10, 2) # [0, 2, 4, 6, 8]
  • np.linspace() → evenly spaced numbers over interval
np.linspace(0, 1, 5) # [0.0, 0.25, 0.5, 0.75, 1.0]
  • np.full() → array filled with a constant value
np.full((2, 3), 7)
  • np.empty() → uninitialized array (fast, values are garbage)
np.empty((3, 3))

🔄 Array Manipulation

  • .reshape() → change shape without changing data
arr.reshape(3, 4)
  • .flatten() / .ravel() → flatten to 1D
arr.flatten()
arr.ravel() # returns view when possible (faster)
  • .T → transpose
matrix.T
  • np.concatenate() → join arrays along existing axis
np.concatenate([a, b], axis=0)
  • np.stack() → join arrays along new axis
np.stack([a, b], axis=0)
  • np.split() → split array into multiple sub-arrays
np.split(arr, 3)
  • np.tile() → repeat array along axes
np.tile(arr, (2, 3))
  • np.newaxis → add a new axis/dimension
arr[:, np.newaxis]

Mathematical Operations

  • np.add(), np.subtract(), np.multiply(), np.divide()
np.add(a, b)
  • np.power() → element-wise power
np.power(arr, 2)
  • np.sqrt() → square root
np.sqrt(arr)
  • np.exp() → exponential
np.exp(arr)
  • np.log(), np.log10(), np.log2() → logarithms
np.log(arr)
  • np.sin(), np.cos(), np.tan() → trigonometric
np.sin(arr)
  • np.abs() → absolute value
np.abs(arr)
  • np.clip() → clip values to a range
np.clip(arr, 0, 10)

📊 Statistics & Aggregation

  • np.sum() → sum of elements
np.sum(arr)
np.sum(arr, axis=0) # sum along axis
  • np.mean() → arithmetic mean
np.mean(arr)
  • np.median() → median
np.median(arr)
  • np.std() / np.var() → standard deviation / variance
np.std(arr)
np.var(arr)
  • np.min() / np.max() → minimum / maximum
np.min(arr)
np.max(arr)
  • np.argmin() / np.argmax() → index of min / max
np.argmin(arr)
np.argmax(arr)
  • np.percentile() → compute percentile
np.percentile(arr, 75) # 75th percentile
  • np.cumsum() / np.cumprod() → cumulative sum / product
np.cumsum(arr)

🔍 Searching & Sorting

  • np.where() → return indices where condition is True
np.where(arr > 5)
np.where(condition, x, y) # ternary: x if condition else y
  • np.sort() → sort array
np.sort(arr)
np.sort(arr, axis=0) # sort along axis
  • np.argsort() → indices that would sort array
np.argsort(arr)
  • np.unique() → unique elements
np.unique(arr)
  • np.in1d() → test membership
np.in1d(arr, [2, 4, 6])

🧮 Linear Algebra

  • np.dot() / @ → dot product / matrix multiplication
np.dot(A, B)
A @ B
  • np.linalg.inv() → matrix inverse
np.linalg.inv(A)
  • np.linalg.det() → determinant
np.linalg.det(A)
  • np.linalg.eig() → eigenvalues and eigenvectors
np.linalg.eig(A)
  • np.linalg.svd() → singular value decomposition
np.linalg.svd(A)
  • np.linalg.solve() → solve linear system Ax = b
np.linalg.solve(A, b)
  • np.linalg.norm() → matrix or vector norm
np.linalg.norm(arr)

🎲 Random

  • np.random.rand() → uniform [0, 1)
np.random.rand(3, 3)
  • np.random.randn() → standard normal
np.random.randn(1000)
  • np.random.randint() → random integers
np.random.randint(0, 10, size=(3, 3))
  • np.random.choice() → random sample from array
np.random.choice([1, 2, 3], size=10)
  • np.random.shuffle() → shuffle array in-place
np.random.shuffle(arr)
  • np.random.seed() → set seed for reproducibility
np.random.seed(42)

💾 I/O Operations

  • np.save() → save array to binary .npy
np.save('array.npy', arr)
  • np.load() → load from .npy
np.load('array.npy')
  • np.savez() → save multiple arrays
np.savez('arrays.npz', a=arr, b=arr * 2)
  • np.savetxt() → save to text file
np.savetxt('data.csv', arr, delimiter=',')
  • np.loadtxt() → load from text file
np.loadtxt('data.csv', delimiter=',')